package com.harth.blemultiplatform.ble.core

/**
 * Enumeration of properties of GATT characteristics.
 *
 * A property defines available actions for a given characteristic.
 */
enum class CharacteristicProperty {
    /**
     * A property that indicates the characteristic can broadcast its value using a characteristic
     * configuration descriptor.
     */
    BROADCAST,
    /** A property that indicates a peripheral can read the characteristic’s value. */
    READ,
    /**
     * A property that indicates a peripheral can write the characteristic’s value,
     * without a response to indicate that the write succeeded.
     */
    WRITE_WITHOUT_RESPONSE,
    /**
     * A property that indicates a peripheral can write the characteristic’s value,
     * with a response to indicate that the write succeeded.
     */
    WRITE,
    /**
     * A property that indicates the peripheral permits notifications of the characteristic’s value,
     * without a response from the central to indicate receipt of the notification.
     */
    NOTIFY,
    /**
     * A property that indicates the peripheral permits notifications of the characteristic’s value,
     * with a response from the central to indicate receipt of the notification.
     */
    INDICATE,
    /**
     * A property that indicates the peripheral allows signed writes of the characteristic’s value,
     * without a response to indicate the write succeeded.
     */
    SIGNED_WRITE,
    /**
     * A property that indicates the characteristic defines additional properties in the extended
     * properties descriptor.
     */
    EXTENDED_PROPERTIES;
    /**
     * A property that indicates the peripheral allows reliable writes of the characteristic’s value.
     *
     * Once reliable write, also known as queued write, is started, all write requests
     * are sent to the remote device for verification and queued up for atomic execution.
     * The application is responsible for verifying whether the value has been transmitted
     * accurately. After all characteristics have been queued up and verified, they may be
     * executed. If a characteristic was not written correctly, aborting reliable write will
     * cancel the current transaction without committing any values on the remote LE device.
     */
    // RELIABLE_WRITE,
    /**
     * A property that indicates the Characteristic User Description descriptor is writable.
     */
    // WRITEABLE_AUXILIARIES,

    /**
     * The write type associated with this property, or null if the property does not
     * relates to writing.
     */
    val writeType: WriteType?
        get() = when (this) {
            WRITE -> WriteType.WITH_RESPONSE
            WRITE_WITHOUT_RESPONSE -> WriteType.WITHOUT_RESPONSE
            SIGNED_WRITE -> WriteType.SIGNED
            else -> null
        }
}

infix fun CharacteristicProperty.and(property: CharacteristicProperty): List<CharacteristicProperty> {
    return listOf(this, property)
}

infix fun List<CharacteristicProperty>.and(property: CharacteristicProperty): List<CharacteristicProperty> {
    return this + property
}